$(function(){
	
	var hideJson = $("#hideJson").val();
	var jsonObject = JSON.parse(hideJson);
	var arr = $(".findByStatus");
	for (var i = 0; i < arr.length; i++) {
		var str = $(arr[i]).attr("data-status") + "";
		var value = jsonObject.searchBeans.ordersStatus.value + "";
		if(str == value){
			$(arr[i]).parent().addClass("active");
			break;
		}
	}
})
$(".findByStatus").click(function(){
	

	var st = $(this).attr("data-status");
	// 获取到基本的json
	var baseJson = createJson();
	// 添加进时间和状态
	// 状态
	var status = {
		"ordersStatus" : {
			"logicType" : 0,
			"value" : st,
			"spareValue" : null
		}
	}
	$.extend(baseJson.searchBeans, status);

	// 按照时间排序
	var sortBean = {
		"operatorTime" : {
			"sortDirection" : "desc",
			"sortPriority" : 0,
		}
	}
	$.extend(baseJson.sortBeans, sortBean);

	var url = base_url + "customer/toRushOrderListPage";
	var params = {
		"jsonCondition" : JSON.stringify(baseJson)
	}
	post(url, params);

})

/**
 * 申请或者取消退款
 * 
 * @param obj
 * @param type
 */
function dealRefund() {

	// 获取到参数
	var ordersItemId = $(".confirm_content").attr("data-ordersItemId");
	var ordersId = $(".confirm_content").attr("data-ordersId");
	var applyOrCancel = $(".confirm_content").attr("data-applyOrCancel");
	var url = base_url + "customer/customeRushDealRefund";
	var hideJson = $("#hideJson").val();
	var reason = $("#reason").val();

	// 参数
	var attr = {
		"ordersId" : ordersId,
		"ordersItemId" : ordersItemId,
		"type" : applyOrCancel,
		"userSearchCondition" : null,
		"jsonCondition" : hideJson,
	}
	var params = {
		"jsonArr" : JSON.stringify(attr),
		"reason" : reason
	}
	post(url, params);
}

/**
 * 申请或者取消退货退款
 * 
 * @param obj
 * @param type
 */
function dealRefundGoods() {

	// 获取到参数
	var ordersItemId = $(".confirm_content").attr("data-ordersItemId");
	var ordersId = $(".confirm_content").attr("data-ordersId");
	var applyOrCancel = $(".confirm_content").attr("data-applyOrCancel");

	var url = base_url + "customer/customerRushDealRefundGoods";
	var hideJson = $("#hideJson").val();
	var reason = $("#reason").val();
	var attr = {
		"ordersId" : ordersId,
		"ordersItemId" : ordersItemId,
		"type" : applyOrCancel,
		"userSearchCondition" : null,
		"jsonCondition" : hideJson,
	}
	var params = {
		"jsonArr" : JSON.stringify(attr),
		"reason" : reason
	}
	post(url, params);

}

/**
 * 
 * @param type
 * 
 */
function tankuang(obj, type, applyOrCancel) {

	// 获取到参数
	var ordersItemId = $(obj).parents(".product").attr("id");
	var ordersId = $(obj).parents(".order").attr("id");
	$(".confirm_content").attr("data-ordersId", ordersId);
	$(".confirm_content").attr("data-ordersItemId", ordersItemId);
	$(".confirm_content").attr("data-type", type);
	$(".confirm_content").attr("data-applyOrCancel", applyOrCancel);

	if (applyOrCancel == 0) {
		//客户端弹框初始化（必须）
		$("#applyOrRefuse").before("<div class='shadow'></div>");
		$("#applyOrRefuse").fadeIn();
		//点击遮罩关闭弹框
		clickShadow(function() {
			$("#applyOrRefuse").fadeOut();
		})
	} else if (applyOrCancel == 1) {// 取消退化

		switch (type) {
		case 0:
			dealRefund();// 退款
			break;
		case 1:
			dealRefundGoods();// 退货
			break;
		default:
			break;
		}
	}
}

// 点击确定按钮
$("#applyOrRefuse").on("click", ".btn", function() {

	var reason = $("#reason").val();
	if (reason == "" || reason == null || reason == undefined) {		
		if(!$(this).hasClass("cancel")){
			alert("理由不能为空");
		}
	} else {
		var type = $(".confirm_content").attr("data-type");
		switch (type) {
		case '0':
			dealRefund();// 退款
			break;
		case '1':
			dealRefundGoods();// 退货
			break;
		default:
			break;
		}
		//点击按钮关闭弹框
		$(".shadow").remove();
		$("#applyOrRefuse").fadeOut();
	}

})

// 点击确定按钮
$("#applyOrRefuse").on("click", ".cancel", function() {

		//点击按钮关闭弹框
		$(".shadow").remove();
		$("#applyOrRefuse").fadeOut();

})

/**
 * 进行签收
 * 
 * @param obj
 */
function sign(obj) {

	// 订单id
	var ordersId = $(obj).parents(".order").attr("id");
	var hideJson = $("#hideJson").val();
	
	var attr = {
		"ordersId" : ordersId,
		"ordersItemId" : null,
		"type" : null,
		"userSearchCondition" : null,
		"jsonCondition" : hideJson,
	}
	var params = {
		"jsonArr" : JSON.stringify(attr)
	}
	var url = base_url + "customer/signRush";
	post(url, params);

}

$(".returnGoodsing").on("tap",function(){
	$("#returnGoodsing-msg").before("<div class='shadow'></div>").fadeIn();
	clickShadow(function() {
		$("#returnGoodsing-msg").fadeOut();
	})
    var name = $(this).parents(".order").attr("data-name");
	var phone= $(this).parents(".order").attr("data-phone");
	var address= $(this).parents(".order").attr("data-address");
	var str = name + phone + address ; 
	if(str==null || str=="" || str==undefined){
		$("#refundGoodTip").html("<p>请联系卖家获取退货信息</p></br></br></br>");
	}else{
		$("#contactName").text("卖家姓名：" +name);
		$("#contactWay").text("卖家电话：" +phone);
		$("#contactAddress").text("买家收货地址："+address );	
	}
	$("#returnGoodOrdersId").attr("value",$(this).parents(".order").attr("id"));
	$("#returnGoodOrdersItemsId").attr("value",$(this).parents(".product").attr("id"));
	
})

/**
 * 进行返货
 */
$(".returnGoodCommit").click(function(e){
	
	// 订单id
	var ordersId = $("#returnGoodOrdersId").attr("value");
	var ordersItemId = $("#returnGoodOrdersItemsId").attr("value");
	var hideJson = $("#hideJson").val();
	//获取到物流公司的消息
	var selectObj = $("#expressCompany")[0];
	var expressCompanyId = selectObj.options[selectObj.selectedIndex].value;
	var transportNumber = $("#transportNumber").val();
	//如果物流单号为空，则不能提交
	if(transportNumber==null || transportNumber=="" ||transportNumber==undefined){ 
		tip.content("物流单号不能为空").show();
	}else{		
		var attr = {
				"ordersId" : ordersId,
				"ordersItemId" : ordersItemId,
				"type" : null,
				"userSearchCondition" : null,
				"jsonCondition" : hideJson,
			}
			var params = {
				"jsonArr" : JSON.stringify(attr),
				"expressCompanyId" : expressCompanyId,
				"transportNumber" : transportNumber
			}
			var url = base_url + "customer/returnRushGoodsing";
			post(url, params);
	}

})


/**
 * 付款
 * 
 * @param obj
 */
function pay(obj) {

	// 订单id
	var ordersId = $(obj).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payOrdersInOrdersList";
	post(url, params);
}

/**
 * 关闭订单
 * 
 * @param obj
 */
function closeOrder(obj) {

	// 订单id
	var ordersId = $(obj).parents(".order").attr("id");
	
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "customer/closeRushOrder";
	post(url, params);

}

/**
 * 退货详情
 */
$(".tuikuan").click(function(){
	
	//客户端弹框初始化（必须）
	$("#detail").before("<div class='shadow'></div>");
	$("#detail").fadeIn();
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#detail").fadeOut();
	})
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var ordersItemId = $(this).parents(".product").attr("id");
	$("#detailContent").html("</p>加载中...</p>");
	//请求成功
	var success = function(data){
	
		$("#detailHead").text("退款详情");
		$("#detailContent").html("");
		if(data.applyReason!=null && data.applyReason!=""){
			
			$("#detailContent").append("</p>申请理由：" + data.applyReason+"</p>");
			$("#detailContent").append("</br>");
		}
        if(data.refuseReason!=null && data.refuseReason!=""){
        	$("#detailContent").append("</p>拒绝理由：" + data.refuseReason+"</p>");
        	$("#detailContent").append("</br>");
		}
	}	
	//请求失败
	var error = function(){
		$("#detailContent").html("</p>请求失败</p>");
	}	
	var url = base_url + "customer/findRushDetail";
	var params = "ordersId="+ordersId+"&type=0&ordersItemId=" + ordersItemId;
	doAjax("POST", url, params, success, error, true, false);
	
})

/**
 * 退款详情
 */
$(".tuihuo").click(function(){

	// 订单id
	var ordersId = $(this).parents(".order").attr("id");	
	var ordersItemId = $(this).parents(".product").attr("id");
	//客户端弹框初始化（必须）
	$("#detail").before("<div class='shadow'></div>");
	$("#detail").fadeIn();
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#detail").fadeOut();
	})
	//请求成功
	var success = function(data){
		$("#detailContent").html("");
		$("#detailHead").text("退货退款详情");		
        if(data.address!=null && data.address!=""){
			
			$("#detailContent").append("</p>退货详情：" + data.address+"</p>");
			$("#detailContent").append("</br>");
		}
		if(data.applyReason!=null && data.applyReason!=""){
			
			$("#detailContent").append("</p>申请理由：" + data.applyReason+"</p>");
			$("#detailContent").append("</br>");
		}
        if(data.refuseReason!=null && data.refuseReason!=""){
        	$("#detailContent").append("</p>拒绝理由：" + data.refuseReason+"</p>");
        	$("#detailContent").append("</br>");
		}
	}	
	//请求失败
	var error = function(){
		$("#detailContent").html("</p>请求失败</p>");
	}	
	var url = base_url + "customer/findRushDetail";
	var params = "ordersId="+ordersId + "&type=1"+"&ordersItemId="+ordersItemId;
	doAjax("POST", url, params, success, error, true, false);
	
})

// 点击确定按钮
$("#detail").on("click", ".btn", function() {

	//点击按钮关闭弹框
	$(".shadow").remove();
	$(".confirm").fadeOut();

})
